package com.zrrd.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zrrd.common.file.service.FileStorageService;
import com.zrrd.common.page.ImagePageVo;
import com.zrrd.common.util.ResponseResult;
import com.zrrd.entity.Image;
import com.zrrd.service.ImageService;
import io.swagger.annotations.Api;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wanjl
 * @since 2024-09-12
 */
@Api(tags = "图片管理")
@RestController
@RequestMapping("/admin")
@ResponseBody
public class ImageController {
    @Resource
    private ImageService imageService;
    @Resource
    private FileStorageService fileStorageService;

    @Value("${minio.endpoint}")
    private String endpoint;

    @Value("${minio.bucket}")
    private String bucket;

    @GetMapping("/imageClass/{id}/image/{page}")
    public ResponseResult findByPage(@PathVariable("id") Integer id,
                                     @PathVariable("page") Integer page,
                                     @RequestParam(value = "limit",required = false,defaultValue = "10") Integer limit,
                                     @RequestParam(value = "ordered",required = false,defaultValue = "desc") String ordered,
                                     @RequestParam(value = "keyword",required = false,defaultValue = "") String keyword){
        PageHelper.startPage(page,limit);
        List<Image> list = imageService.lambdaQuery()
                .eq(Image::getImageClassId, id)
                .like(!keyword.equals(""), Image::getName, keyword)
                .orderBy(ordered != null, ordered.equals("asc") ? true : false, Image::getName)
                .list();
        PageInfo<Image> pageInfo = new PageInfo<>(list);

        ImagePageVo imagePageVo = new ImagePageVo(pageInfo.getList(),pageInfo.getTotal());
        return ResponseResult.queryResponse(imagePageVo);
    }
    @PostMapping("/image/upload")
    public ResponseResult upload(@RequestParam("imageClassId") Integer imageClassId,MultipartFile file) throws IOException {
        //上传图片
        Image image = this.upload(file, imageClassId);
        //把图片路径保存到数据库
        boolean flag = imageService.save(image);
        return ResponseResult.saveResponse(flag);
    }


    private Image upload(MultipartFile file,Integer imageClassId) throws IOException {
        //上传
        InputStream inputStream = file.getInputStream();
        String name = file.getOriginalFilename();
        String imageFile = fileStorageService.uploadImageFile("", name, inputStream);
        Image image = new Image()
                .setImageClassId(imageClassId)
                .setUrl(endpoint+"/"+bucket+imageFile)
                .setName(imageFile.substring(imageFile.lastIndexOf("/") + 1))
                .setPath(imageFile);
        return image;
    }
    @PutMapping("/image")
    public ResponseResult update(@RequestBody Image image){
        boolean flag = imageService.updateById(image);
        return ResponseResult.updateResponse(flag);
    }
    @DeleteMapping("/image/{id}")
    public ResponseResult delete(@PathVariable("id") Integer id){
        Boolean flag = remove(id);
        return ResponseResult.updateResponse(flag);
    }
    private Boolean remove(Integer id){
        Image image = imageService.getById(id);
        fileStorageService.deleteFile(image.getPath());
        boolean flag = imageService.removeById(id);
        return flag;
    }

    @PostMapping("/image/deleteAll")
    public ResponseResult deleteAll(@RequestBody List<Integer> ids){
        Boolean flag=false;
        for (Integer id : ids) {
            flag= remove(id);
            if (!flag) return ResponseResult.deleteResponse(flag);
        }
        return ResponseResult.deleteResponse(flag);
    }



}

